<template>
  <div class="login_container">
    <!-- 背景图 -->
    <img src="../assets/images/bg.png" alt="登录背景图" class="wave" />
    <div class="container">
      <!-- 左侧大图 -->
      <div class="left_img">
        <img src="../assets/images/left_bg.svg" alt="左侧大图" />
      </div>
      <!-- 表单 -->
      <div class="login-box">
        <el-form
          :model="loginForm"
          :rules="loginFormRules"
          ref="LoginFormRef"
          label-width="0px"
          class="login_form"
        >
          <img src="../assets/images/avatar.svg" alt="" class="avatar" />
          <h2>欢迎</h2>
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              prefix-icon="iconfont icon-user"
            >
            </el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="loginForm.password"
              prefix-icon="iconfont icon-3702mima"
            ></el-input>
          </el-form-item>
          <el-button :loading="loginLoading" class="btn" @click="login" round
            >登录</el-button
          >
          <!-- <el-button class="btn" @click="resetLoginForm">重置</el-button> -->
        </el-form>
      </div>
    </div>
    <a href="javascript:void(0)" target="_blank" class="copyright"
      >&copy; FeiYiYi</a
    >
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginLoading: false,
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '登录名长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // resetLoginForm() {
    //   this.$refs.LoginFormRef.resetFields()
    // },
    login() {
      this.$refs.LoginFormRef.validate(async (valid) => {
        console.log(this.loginFormRules)
        if (!valid) {
          return
        }
        this.loginLoading = true
        const { data: res } = await this.$http.post('login', this.loginForm)
        this.loginLoading = false
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败:' + res.meta.msg)
        }
        this.$message.success('登录成功')
        console.log(res)
        // 保存token
        window.sessionStorage.setItem('token', res.data.token)
        // 导航至/home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  position: relative;
  height: 100%;
  .wave {
    position: fixed;
    height: 100%;
    left: 0;
    bottom: 0;
    z-index: -1;
  }
  .container {
    width: 100vw;
    height: 100vh;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 18rem;
    padding: 0 2rem;
    .left_img {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      img {
        width: 500px;
      }
    }
    .login-box {
      display: flex;
      align-items: center;
      text-align: center;
      .login_form {
        width: 360px;
        h2 {
          font-size: 2.9rem;
          text-transform: uppercase;
          margin: 15px 0;
          color: #999;
        }
      }
      .avatar {
        width: 100px;
      }
      .btn {
        display: block;
        width: 100%;
        margin: 1rem 0;
        font-size: 1.2rem;
        outline: none;
        border: none;
        background-image: linear-gradient(to right, #32be8f, #38d39f, #32be8f);
        color: #fff;
        background-size: 200%;
        transition: 0.5s;
        &:hover {
          background-position: right;
        }
      }
    }
  }
  .copyright {
    position: absolute;
    -webkit-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    transform: translateX(-50%);
    left: 50%;
    bottom: 50px;
    color: #38d39f;
    text-align: center;
    font-size: 16px;
    text-decoration: none;
  }
}
/*媒体查询*/
@media screen and (max-width: 1080px) {
  .login_container {
    .container {
      grid-gap: 9rem;
    }
  }
}
@media screen and (max-width: 1024px) {
  .login_container {
    .container {
      .left_img {
        img {
          width: 360px;
        }
      }
      .login_form {
        width: 290px;
        h2 {
          font-size: 2.4rem;
          margin: 8px 0;
        }
      }
    }
  }
}
@media screen and (max-width: 768px) {
  .login_container {
    .wave {
      display: none;
    }
    .container {
      grid-template-columns: 1fr;
      .left_img {
        display: none;
      }
      .login-box {
        justify-content: center;
      }
    }
  }
}
</style>
